/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.member.service;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.juhe.AesUtil;
import com.jeeplus.juhe.RNAUtil;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.address.entity.Address;
import com.jeeplus.modules.address.service.AddressService;
import com.jeeplus.modules.dynamicrecord.entity.DynamicRecord;
import com.jeeplus.modules.dynamicrecord.service.DynamicRecordService;
import com.jeeplus.modules.freezingassets.entity.FreezingAssets;
import com.jeeplus.modules.freezingassets.service.FreezingAssetsService;
import com.jeeplus.modules.integralorders.service.IntegralOrdersService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.mapper.MemberMapper;
import com.jeeplus.modules.partitioninfo.entity.PartitionInfo;
import com.jeeplus.modules.partitioninfo.service.PartitionInfoService;
import com.jeeplus.modules.positionassets.entity.PositionAssets;
import com.jeeplus.modules.positionassets.service.PositionAssetsService;
import com.jeeplus.modules.recharge.entity.Recharge;
import com.jeeplus.modules.recharge.service.RechargeService;
import com.jeeplus.modules.sessions.entity.Sessions;
import com.jeeplus.modules.sessions.service.SessionsService;
import com.jeeplus.modules.subgoods.entity.SubGoods;
import com.jeeplus.modules.subgoods.service.SubGoodsService;
import com.jeeplus.modules.subrecord.entity.SubRecord;
import com.jeeplus.modules.subrecord.service.SubRecordService;
import com.jeeplus.modules.tixian.entity.Tixian;
import com.jeeplus.modules.tixian.service.TixianService;
import com.jeeplus.modules.traderecord.entity.TradeRecord;
import com.jeeplus.modules.traderecord.mapper.TradeRecordMapper;
import com.jeeplus.modules.traderecord.service.TradeRecordService;
import com.jeeplus.modules.userbank.entity.Userbank;
import com.jeeplus.modules.userbank.service.UserbankService;
import com.jeeplus.modules.userpay.entity.Userpay;
import com.jeeplus.modules.userpay.service.UserpayService;
import com.jeeplus.modules.userwx.entity.Userwx;
import com.jeeplus.modules.userwx.service.UserwxService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息Service
 * 
 * @author freedom
 * @version 2020-08-23
 */
@Service
@Transactional(readOnly = true)
public class MemberService extends CrudService<MemberMapper, Member> {

	@Autowired
	private AccountService accountService;
	@Autowired
	private AddressService addressService;
	@Autowired
	private UserbankService userbankService;
	@Autowired
	private UserpayService userpayService;
	@Autowired
	private UserwxService userwxService;
	@Autowired
	private RechargeService rechargeService;
	@Autowired
	private TixianService tixianService;
	@Autowired
	private PartitionInfoService partitionInfoService;
	@Autowired
	private PositionAssetsService positionAssetsService;
	@Autowired
	private FreezingAssetsService freezingAssetsService;
	@Autowired
	private SubRecordService subRecordService;
	@Autowired
	private DynamicRecordService dynamicRecordService;
	@Autowired
	private SubGoodsService subGoodsService;
	@Autowired
	private TradeRecordService tradeRecordService;
	@Autowired
	private TradeRecordMapper tradeRecordMapper;


	public Member get(String id) {
		return super.get(id);
	}

	public List<Member> findList(Member member) {
		return super.findList(member);
	}

	public Page<Member> findPage(Page<Member> page, Member member) {
		String managerId = "";
		dataRuleFilter(member);
		member.setPage(page);
		if (member.getTeamManager() != null){
			managerId = member.getTeamManager().getId();
		}
		if (StringUtils.isNotBlank(managerId)){

			Member member1 = new Member();
			member1.setP(member.getTeamManager());
//			member.setP(member.getManager());
			List<Member> memberList = findList(member1);
			//查询团队下级人员
			List<Member> queryList = new ArrayList<>();
			//最终集合
			List<Member> list = new ArrayList<>();
			queryList.addAll(memberList);
			list.addAll(memberList);
			while (list.size() > 0) {
				list.clear();
				List<Member> list1 = new ArrayList<>();
				for (Member member2 : queryList) {
					Member entity = new Member();
					entity.setP(member2);
//					member.setP(member2);
					List<Member> memberList1 = findList(entity);
					memberList.addAll(memberList1);
					list1.addAll(memberList1);
				}
				list.addAll(list1);
				queryList.clear();
				queryList.addAll(list1);
			}
			page.setCount(memberList.size());
			page.setList(memberList);
		}else {
			List<Member> list = mapper.findMemberList(member);
//		if (list.size()>0){
//			for (Member member1 : list) {
//				Account account = accountService.get(member1.getAccount().getId());
//				member1.setTeamNum(account.getNum());
//			}
//		}
			page.setList(list);
		}

		return page;
//		return super.findPage(page, member);
	}

	@Transactional(readOnly = false)
	public void save(Member member) {
		super.save(member);
	}

	public Integer getTodayNum() {
		Integer num = mapper.getTodayNum();
		return num;
	}
	@Transactional(readOnly = false)
	public void delete(Member member) {
		Account entity = new Account();
		entity.setMember(member);
		List<Account> account = accountService.findList(entity);
		if (account.size()>0){
			accountService.delete(account.get(0));
		}
		Address address = new Address();
		address.setMember(member);
		List<Address> addressList = addressService.findList(address);
		if (addressList.size()>0){
			addressService.delete(addressList.get(0));
		}
		Userbank userbank = new Userbank();
		userbank.setMember(member);
		List<Userbank> userbankList = userbankService.findList(userbank);
		if (userbankList.size()>0){
			userbankService.delete(userbankList.get(0));
		}
		Userpay userpay = new Userpay();
		userpay.setMember(member);
		List<Userpay> userpayList = userpayService.findList(userpay);
		if (userpayList.size()>0){
			userpayService.delete(userpayList.get(0));
		}
		Userwx userwx = new Userwx();
		userwx.setMember(member);
		List<Userwx> userwxList = userwxService.findList(userwx);
		if (userwxList.size()>0){
			userwxService.delete(userwxList.get(0));
		}
		Recharge recharge = new Recharge();
		recharge.setMemberId(member.getId());
		List<Recharge> rechargeList = rechargeService.findList(recharge);
		if (rechargeList.size()>0){
			for (Recharge recharge1 : rechargeList) {
				rechargeService.delete(recharge1);
			}
		}
		Tixian tixian = new Tixian();
		tixian.setMember(member);
		List<Tixian> tixianList = tixianService.findList(tixian);
		if (tixianList.size()>0){
			for (Tixian tixian1 : tixianList) {
				tixianService.delete(tixian1);
			}
		}
		PartitionInfo partitionInfo = new PartitionInfo();
		partitionInfo.setMember(member);
		List<PartitionInfo> partitionInfoList = partitionInfoService.findList(partitionInfo);
		if (partitionInfoList.size()>0){
			partitionInfoService.deleteAllByLogic(partitionInfoList);
		}
		
		super.delete(member);
	}

	public Member getMemberByLoginName(String loginName) {
		return mapper.getMemberByLoginName(loginName);
	}

	public Member getMemberByCode(String code) {
		return mapper.getMemberByCode(code);
	}

	/**
	 * 前端注册用户
	 * 
	 * @param member
	 */
	@Transactional(readOnly = false)
	public void saveNewMember(Member member) {
		member.setNum(0);
		if (member.getP() != null){
			Member upMem = get(member.getP().getId());
			if (upMem != null){
				Integer num = upMem.getNum()+1;
				upMem.setNum(num);
				System.out.println("直推人数========="+upMem.getNum());
				save(upMem);
				manageMemberType();
			}
			while (upMem != null){
				Account account = accountService.get(upMem.getAccount().getId());
				account.setNum(account.getNum() + 1);
				accountService.save(account);
				if (upMem.getP() != null){
					upMem = get(upMem.getP().getId());
				}else {
					break;
				}
			}
		}
		if (member.getP() != null){
			Member pMember = get(member.getP().getId());
			while (pMember != null){
				if (!"7359709a335b4053b2b470f9c54764a7".equals(pMember.getId())){
					if (pMember.getP() != null){
						if ("7359709a335b4053b2b470f9c54764a7".equals(pMember.getP().getId())){
							member.setTeamManager(pMember);
							break;
						}else {
							pMember = get(pMember.getP().getId());
						}
					}else {
						member.setTeamManager(pMember);
						break;
					}
				}else {
					break;
				}
			}
		}
		member.setUsertype("0");
		member.setTgcode(generateCode());
		member.setIsIntegral("0");
		member.setIsService("0");
		member.setIsCollection("1");
		member.setIsIgnore("0");
//		member.setIdcode("11111");
//		member.setName("仨妞儿商城");
//		member.setConfirmFlag("1");
		super.save(member);
		Account account = new Account();
		account.setMember(member);
		account.setYue(0.0);
		account.setBuy(0.0);
		account.setTeam(0.0);
		account.setValid(0.0);
		account.setNum(0);
		account.setTixian(0.0);
		account.setDynamicProfit(0.0);
		account.setTodayDynamic(0.0);
		account.setTeamProfit(0.0);
		account.setTodayTeam(0.0);
		account.setTotalProfit(0.0);
		account.setTodayProfit(0.0);
		account.setIntegral(0.0);
		account.setTeamService(0.0);
		account.setExchangeCoupon(0.0);
		accountService.save(account);

		member.setAccount(account);
		super.save(member);
	}

	/**
	 * 后端注册用户
	 * 
	 * @param member
	 */
	@Transactional(readOnly = false)
	public void saveNew(Member member) {
		Member mem = getMemberByCode(member.getTgcode());
		member.setIsIntegral("0");
		member.setIsService("0");
		member.setTgcode(generateCode());
		member.setIsCollection("1");
		member.setIsIgnore("0");
//		member.setIdcode("11111");
//		member.setName("仨妞儿商城");
//		member.setConfirmFlag("1");

		member.setP(mem);

		if (member.getP() != null){
			Member upMem = get(member.getP().getId());
			if (upMem != null){
				upMem.setNum(upMem.getNum() + 1);
				System.out.println(upMem.getNum() + 1);
				save(upMem);
			}
			while (upMem != null){
				Account account = accountService.get(upMem.getAccount().getId());
				account.setNum(account.getNum() + 1);
				accountService.save(account);
				if (upMem.getP() != null){
					upMem = get(upMem.getP().getId());
				}else {
					break;
				}
			}
		}
		member.setP(null);
		super.save(member);
		Account account = new Account();
		account.setMember(member);
		account.setYue(0.0);
		account.setBuy(0.0);
		account.setTeam(0.0);
		account.setValid(0.0);
		account.setNum(0);
		account.setTixian(0.0);
		account.setDynamicProfit(0.0);
		account.setTodayDynamic(0.0);
		account.setTeamProfit(0.0);
		account.setTodayTeam(0.0);
		account.setTotalProfit(0.0);
		account.setTodayProfit(0.0);
		account.setIntegral(0.0);
		account.setTeamService(0.0);
		account.setExchangeCoupon(0.0);
		accountService.save(account);
		member.setAccount(account);
		super.save(member);
	}

	/**
	 * 生成6位数字邀请码
	 * 
	 * @return
	 */
	private String generateCode() {
		String code = "";
		int count = 1;
		while (count > 0) {
			int random = (int) ((Math.random() * 9 + 1) * 100000);
			code = random + "";
			count = mapper.getCountByCode(code);
		}
		return code;
	}

	

	public List<Member> findListAll(Member member) {
		return mapper.findList(member);
	}


	@Transactional(readOnly = false)
	public void confirmIdCode(Member member) {
//		if ("0".equals(member.getConfirmFlag())){
			String id = member.getId();
			mapper.confirmIdCode(id);
			//审核通过实名认证  余额+20
//			Account account = new Account();
//			account.setMember(member);
//			List<Account> accountList = accountService.findList(account);
//			if (accountList.size() > 0){
//				accountList.get(0).setYue(BigDecimal.valueOf(accountList.get(0).getYue()).add(BigDecimal.valueOf(20)).doubleValue());
//				accountService.save(accountList.get(0));
//			}
//			//插入记录
//			DynamicRecord dynamicRecord = new DynamicRecord();
//			dynamicRecord.setMember(member);
//			dynamicRecord.setMoney(20d);
//			dynamicRecord.setName("注册奖");
//			dynamicRecord.setFrommem(member);
//			dynamicRecordService.save(dynamicRecord);
//		}
	}


	/**
	 * 实名认证
	 * @param member
	 * @return
	 */
	@Transactional(readOnly = false)
	public AjaxJson checkID(Member member,String realname,String idcard) {
		AjaxJson aj = new AjaxJson();
//		String realname = member.getName();// 姓名
//		String idcard = member.getIdcode();// 身份证
		String name = realname;
		String idCode = idcard;
		String key = RNAUtil.MD5(RNAUtil.OPENID).substring(0, 16);
		int type = 3;// 加密版本
		realname = AesUtil.encrypt(realname, key);
		idcard = AesUtil.encrypt(idcard, key);
		Map<String, Object> params = new HashMap<>();
		params.put("realname", realname);
		params.put("idcard", idcard);
		String result = null;
		try {
			result = RNAUtil.queryResult(params, type);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(result);

		JSONObject json = JSONObject.fromObject(result);
		String error_code = json.getString("error_code");
		if("0".equals(error_code)) {
			JSONObject info = JSONObject.fromObject(json.get("result"));
			int code = info.getInt("res");
			if(code == 1) {
				//若是第一次认证则认定为新邀请用户，给上级直推人数+1
				if (StringUtils.isBlank(member.getIdcode()) && StringUtils.isBlank(member.getName())){
					if (member.getP() != null){
						Member upMem = get(member.getP().getId());
						if (upMem != null){
							upMem.setNum(upMem.getNum() + 1);
							save(upMem);
						}
						while (upMem != null){
							Account account = accountService.get(upMem.getAccount().getId());
							account.setNum(account.getNum() + 1);
							accountService.save(account);
							if (upMem.getP() != null){
								upMem = get(upMem.getP().getId());
							}else {
								break;
							}
						}
					}
				}

				member.setName(name);
				member.setIdcode(idCode);
//				member.setIdcodeImgFront(idcodeImgFront);
				member.setIdcodeImgBehind("1");
//				save(member);
//				aj.setSuccess(true);
//				aj.setMsg("实名信息已上传");
				member.setConfirmFlag("1");
				save(member);
				//审核通过实名认证  余额+20
//				Account account = new Account();
//				account.setMember(member);
//				List<Account> accountList = accountService.findList(account);
//				if (accountList.size() > 0){
//					accountList.get(0).setYue(BigDecimal.valueOf(accountList.get(0).getYue()).add(BigDecimal.valueOf(20)).doubleValue());
//					accountService.save(accountList.get(0));
//				}
//				//插入记录
//				DynamicRecord dynamicRecord = new DynamicRecord();
//				dynamicRecord.setMember(member);
//				dynamicRecord.setMoney(20d);
//				dynamicRecord.setName("注册奖");
//				dynamicRecord.setFrommem(member);
//				dynamicRecordService.save(dynamicRecord);

				aj.setSuccess(true);
				aj.setMsg("实名认证成功！");
			}else {
				aj.setSuccess(false);
				aj.setMsg("姓名与身份证号不匹配");
			}
		}else {
			String reason = json.getString("reason");
			aj.setSuccess(false);
			aj.setMsg(reason);
		}
		return aj;
	}


	@Transactional(readOnly = false)
	public void rejectIdCode(Member member) {
//		if ("0".equals(member.getConfirmFlag())){
			String id = member.getId();
			mapper.rejectIdCode(id);
//		}
	}

	public Page<Member> findMembers(Page<Member> page, String orderNo, Member member) {
		dataRuleFilter(member);
		member.setPage(page);
		List<Member> list = mapper.findMemberList(member);
		page.setList(list);
		return page;
	}

	public List<Member> findCenterMembers() {
		return mapper.findCenterMembers();
	}

	/**
	 * 处理用户等级
	 */
	@Transactional(readOnly = false)
	public void manageMemberType() {
		List<Member> members = findListAll(new Member());
		if (members.size()>0){
			for (Member mem : members) {
				if (!"6".equals(mem.getRemarks())){
					List<Member> inviteList = new ArrayList<>();
					//查询用户直推
					Member m = new Member();
					m.setP(mem);
					List<Member> listAll = findListAll(m);
					if (listAll.size()>0){
						for (Member member : listAll) {
							boolean isEffective = isOrNotEffective(member);
							if (isEffective){
								inviteList.add(member);
							}
						}
					}
					int invite = inviteList.size();
					//查询持仓数
					PositionAssets positionAssets = new PositionAssets();
					positionAssets.setMember(mem);
					positionAssets.setIsApply("0");
					List<PositionAssets> list = positionAssetsService.findListAll(positionAssets);
					Integer positionAmount = list.size();
					//查询业绩
					Account account = accountService.get(mem.getAccount().getId());
					Double team = account.getTeam();
					//直推是2星会员个数
					long secondAmount = 0;
					long thirdAmount = 0;
					if (invite >= 10 && team >= 300000){
						//查直推等级有几个2星
						Member member = new Member();
						member.setP(mem);
						List<Member> memberList = findListAll(member);
						if (memberList.size()>0){
							secondAmount = memberList.stream().filter(member1 -> "2".equals(member1.getUsertype())).count();
							thirdAmount = memberList.stream().filter(member1 -> "3".equals(member1.getUsertype())).count();
						}
						if (secondAmount >= 3){
							if (positionAmount>=1){
								mem.setUsertype("3");
							}
						}else {
							if (positionAmount>=1){
								mem.setUsertype("2");
							}
						}

						if (thirdAmount >= 3){
							mem.setIsService("1");
						}

					}else if (invite >= 1 && invite <= 10 || invite >= 10 && team < 300000){
						mem.setUsertype("1");
					}else {
						mem.setUsertype("0");
					}
					save(mem);
				}
			}
		}
	}

	public List<Member> findAllPlaceMembers(String regionPlace) {
		return mapper.findAllPlaceMembers(regionPlace);
	}


	public Page<Member> findBonusPage(Page<Member> memberPage, Member member) {
		dataRuleFilter(member);
		member.setPage(memberPage);
		List<Member> alist = mapper.findList(member);
		if (alist.size()>0){
			for (Member m : alist) {
				//总资产
				Double totalAssets = 0.0;
				//持仓
				Double position = 0.0;
				//锁仓
				Double freezing = 0.0;
				//分红数字
				double star = 0.0;
				if (m != null) {
					Member member1 = new Member();
					member1.setP(m);
					List<Member> memberList = findList(member1);
					//查询团队下级人员
					List<Member> queryList = new ArrayList<>();
					//最终集合
					List<Member> list = new ArrayList<>();
					queryList.addAll(memberList);
					list.addAll(memberList);
					while (list.size() > 0) {
						list.clear();
						List<Member> list1 = new ArrayList<>();
						for (Member member2 : queryList) {
							Member entity = new Member();
							entity.setP(member2);
							List<Member> memberList1 = findList(entity);
							memberList.addAll(memberList1);
							list1.addAll(memberList1);
						}
						list.addAll(list1);
						queryList.clear();
						queryList.addAll(list1);
					}
					for (Member member2 : memberList) {
						if ("2".equals(member2.getUsertype())) {
							star += 0.5;
						} else if ("3".equals(member2.getUsertype())) {
							star += 1;
						}
					}
					//钱包信息
					Account account = accountService.get(m.getAccount().getId());
					if (account != null) {
						//持仓资产
						PositionAssets positionAssets = new PositionAssets();
						positionAssets.setMember(m);
						positionAssets.setIsApply("0");
						List<PositionAssets> positionAssetsList = positionAssetsService.findList(positionAssets);
						if (positionAssetsList.size() > 0) {
							position = positionAssetsList.stream().mapToDouble(PositionAssets::getNowTotal).sum();
						}
						//锁仓
						FreezingAssets freezingAssets = new FreezingAssets();
						freezingAssets.setMember(m);
						freezingAssets.setIsApply("0");
						List<FreezingAssets> freezingAssetsList = freezingAssetsService.findList(freezingAssets);
						if (freezingAssetsList.size() > 0) {
							freezing = freezingAssetsList.stream().mapToDouble(FreezingAssets::getNowTotal).sum();
						}
						totalAssets = account.getYue() + position + freezing;
						star = BigDecimal.valueOf(totalAssets).divide(BigDecimal.valueOf(2500), 1, BigDecimal.ROUND_HALF_DOWN).add(BigDecimal.valueOf(star)).doubleValue();
						m.setBonus(star);
					}

				}
			}
		}
		memberPage.setList(alist);
		return memberPage;
	}

	/**
	 * 是否有效会员
	 * @param mem
	 */
	@Transactional(readOnly = false)
	public boolean isOrNotEffective(Member mem) {
		//是否充值了500以上  12.10改是否买入过一笔  且持仓或锁仓有一笔
		TradeRecord tradeRecord = new TradeRecord();
		tradeRecord.setPurchaseMember(mem);
		List<TradeRecord> list = tradeRecordMapper.findList(tradeRecord);
		//持仓不为0的
		PositionAssets positionAssets = new PositionAssets();
		positionAssets.setMember(mem);
		List<PositionAssets> assetsList = positionAssetsService.findNonList(positionAssets);
		//锁仓不为0的
		FreezingAssets freezingAssets = new FreezingAssets();
		freezingAssets.setMember(mem);
		List<FreezingAssets> nonList = freezingAssetsService.findNonList(freezingAssets);
		if (list.size() > 0 && (assetsList.size() > 0 || nonList.size() > 0)){
			return true;
		}
		return false;
	}

	public Page<Member> findTeamPage(Page<Member> memberPage, Member member) {
		dataRuleFilter(member);
		member.setPage(memberPage);
		Member member1 = new Member();
		member1.setId("7359709a335b4053b2b470f9c54764a7");
		member.setP(member1);
		List<Member> alist = mapper.findList(member);
		Double teamSumSubPro = 0.0;
		Double teamSumSub = 0.0;
		if (alist.size()>0){
			for (Member member2 : alist) {
				StringBuilder sub = new StringBuilder();
				StringBuilder subNum = new StringBuilder();
				Member member3 = new Member();
				member3.setP(member2);
				List<Member> memberList = findList(member3);
				//查询团队下级人员
				List<Member>  queryList = new ArrayList<>();
				List<Member> list = new ArrayList<>();
				queryList.addAll(memberList);
				list.addAll(memberList);
				while (list.size()>0){
					list.clear();
					List<Member> list1 = new ArrayList<>();
					for (Member member4 : queryList) {
						Member entity = new Member();
						entity.setP(member4);
						List<Member> memberList1 = findList(entity);
						memberList.addAll(memberList1);
						list1.addAll(memberList1);
					}
					list.addAll(list1);
					queryList.clear();
					queryList.addAll(list1);
				}
				if (memberList.size()>0){
					for (Member mem : memberList) {
						SubRecord subRecord = new SubRecord();
						subRecord.setMember(mem);
						List<SubRecord> subRecords = subRecordService.findList(subRecord);
						if (subRecords.size()>0){

							teamSumSub += subRecords.stream().mapToDouble(SubRecord::getTotalPrice).sum();

						}

						DynamicRecord dynamicRecord = new DynamicRecord();
						dynamicRecord.setMember(mem);
						List<DynamicRecord> recordList = dynamicRecordService.findList(dynamicRecord);
						if (recordList.size()>0){
							teamSumSubPro += recordList.stream().mapToDouble(DynamicRecord::getMoney).sum();
						}
					}
					List<SubGoods> list1 = subGoodsService.findList(new SubGoods());
					if (list1.size()>0){
						for (SubGoods subGoods : list1) {
							Double num = 0.0;
							sub.append("/").append(subGoods.getGoodsName());
							for (Member mem : memberList) {
								SubRecord subRecord = new SubRecord();
								subRecord.setMember(mem);
								subRecord.setSubGoods(subGoods);
								List<SubRecord> subRecords = subRecordService.findList(subRecord);
								if (subRecords.size()>0){
									num += subRecords.stream().mapToDouble(SubRecord::getGoodsNum).sum();
								}
							}
							subNum.append("/").append(num);
						}
					}
				}
				member2.setTeamSumSub(teamSumSub);
				member2.setTeamSumSubPro(teamSumSubPro);
				member2.setSub(sub.toString());
				member2.setSubNum(subNum.toString());
				teamSumSub = 0.0;
				teamSumSubPro = 0.0;
			}
		}
		memberPage.setList(alist);
		return memberPage;
	}

	public Integer findLevelCount(Member member) {
		String usertype = member.getUsertype();
		int count = 0;
		if (member.getP() != null) {
			Member pMember = get(member.getP().getId());
			if (pMember != null) {
				while (pMember != null){
					if (usertype.equals(pMember.getUsertype())){
						count ++;
					}
					if (pMember.getP() != null){
						pMember = get(pMember.getP().getId());
					}else {
						break;
					}
				}
			}
		}
		return count;
	}
}